package mSocket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * @author Ajie
 * @date 2023/1/29
 * @function
 */
public class UDPMulticastSocket_Client {
    public static void main(String[] args) throws IOException {
        // Create a multicast socket on a specific port
        MulticastSocket socket = new MulticastSocket(5555);

        // Specify the multicast group to join
//        InetAddress group = InetAddress.getByName("230.0.0.1");
        InetAddress group = InetAddress.getLocalHost();
        socket.joinGroup(group);

        // Prepare the message to send
        byte[] message = "Hello, Multicast World!".getBytes();
        DatagramPacket packet = new DatagramPacket(message, message.length, group, 5555);

        System.out.println("UDPMulticastSocket_Client:main:start send");
        // Send the packet
        socket.send(packet);

        // Leave the multicast group
        socket.leaveGroup(group);

        // Close the socket
        socket.close();
        System.out.println("UDPMulticastSocket_Client:main: send over...");
    }


}